package com.hnas.core.util;

//public class DateUtil {

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

import com.mysql.jdbc.Connection;
/**
 * @author Tan Tailiang
 * 创建日期：2006-4-21
 * 类说明：<p>对日期进行操作类</p>
 */
public class DateUtil {
    /**
     * 
     */
	private static DateFormat dateTimeFormatter = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat dateFormatter1 = new SimpleDateFormat("yyyyMMdd");
	private static final long MSECONDS_OF_ONE_DAY = 60 * 60 * 1000 * 24;
	
	private static final long MSECONDS_OF_ONE_MINUTE=60*1000;
	

	private static final String[] days = {"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
	
	private static DateFormat dateTimeFormatter4 = new SimpleDateFormat(
			"HH:mm:ss");
	private static DateFormat dateTimeFormatter1 = new SimpleDateFormat(
			"HHmmss");
	private static DateFormat dateTimeFormatter2 = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	private static DateFormat dateTimeFormatter3= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	
	private static DateFormat dateTimeFormatter5 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm");
	
	/**
	 * write by steven.shi 2004-10-18
	 * 方法说明：<p>取得指定月份的第一天</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getFirstDayOfMonth(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONTH, month - 1);
		cl.set(Calendar.DAY_OF_MONTH, 1);
		return cl.getTime();
	}
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString1(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter1.format(date).trim();

	}
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString4(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter4.format(date).trim();

	}
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString3(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter3.format(date).trim();

	}
	/**
	 * 
	 * 方法说明：<p>取得指定月份的最后一天</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getLastDayOfMonth(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONTH, month - 1);
		cl.set(Calendar.DAY_OF_MONTH, cl
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		return cl.getTime();
	}
	/**
	 * 
	 * 方法说明：<p>将String型的日期转换成Date型</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param dateString
	 * @param format
	 * @param locale
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDateTime(String dateString) throws ParseException 
	{
		Date date = dateTimeFormatter.parse(dateString);
		return date;
	}
	/**
	 * 
	 * 方法说明：<p>将String型的日期转换成Date型</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param dateString
	 * @param format
	 * @param locale
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDateTime3(String dateString) throws ParseException 
	{
		Date date = dateTimeFormatter3.parse(dateString);
		return date;
	}
	/**
	 * 
	 * 方法说明：<p>将String型的日期转换成Date型</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param dateString
	 * @param format
	 * @param locale
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String dateString,String format,Locale locale) throws ParseException 
	{
		SimpleDateFormat formatter = new SimpleDateFormat(format,locale);
		return formatter.parse(dateString);
	}
	
	/**
	 * 
	 * 方法说明：<p>将String型的日期转换成Date型</p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param dateString
	 * @param format
	 * @param locale
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String dateString,String format) throws ParseException 
	{
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.parse(dateString);
	}

	/**
	 * write by steven.shi 2004-10-18
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param orgDate
	 * @return
	 */
	public static Date convertOrgDate(String orgDate) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, Integer.parseInt(orgDate.substring(0, 4)));
		cl.set(Calendar.MONTH, Integer.parseInt(orgDate.substring(5, 7)) - 1);
		cl.set(Calendar.DAY_OF_MONTH, Integer
				.parseInt(orgDate.substring(8, 10)));
		cl.set(Calendar.HOUR_OF_DAY, 0);
		cl.set(Calendar.MINUTE, 0);
		cl.set(Calendar.SECOND, 0);
		return cl.getTime();
	}

	/**
	 * write by steven.shi 2004-10-18
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param orgDate
	 * @return
	 */
	public static Date convertRealTimeDate(String orgDate) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, Integer.parseInt(orgDate.substring(0, 4)));
		cl.set(Calendar.MONTH, Integer.parseInt(orgDate.substring(5, 7)) - 1);
		cl.set(Calendar.DAY_OF_MONTH, Integer
				.parseInt(orgDate.substring(8, 10)));
		return cl.getTime();
	}

	/**
	 * Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:YYYY-MM-DD YYYY:M:D YYYY/M/DD
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String getNextDate(String date) {

		Calendar cd = Calendar.getInstance();
		StringTokenizer token = new StringTokenizer(date, "-/ :");
		if (token.hasMoreTokens()) {
			cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.YEAR, 1970);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
		} else {
			cd.set(Calendar.MONTH, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.DAY_OF_MONTH, 1);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.HOUR_OF_DAY, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MINUTE, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.SECOND, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MILLISECOND, 0);
		}

		long nextTime = cd.getTimeInMillis() + 24 * 60 * 60 * 1000;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date(nextTime));
	}

    /**
     * Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
     * 方法说明：<p></p>
     * 创建日期：2006-4-21
     * 创建人： Tan Nailiang
     * @param date
     * @return
     */
	public static Date stringToDate(String date) {
		if (date == null)
			return null;

		Calendar cd = Calendar.getInstance();
		StringTokenizer token = new StringTokenizer(date, "-/ :");
		if (token.hasMoreTokens()) {
			cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.YEAR, 1970);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
		} else {
			cd.set(Calendar.MONTH, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.DAY_OF_MONTH, 1);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.HOUR_OF_DAY, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MINUTE, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.SECOND, 0);
		}
		if (token.hasMoreTokens()) {
			cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
		} else {
			cd.set(Calendar.MILLISECOND, 0);
		}

		return cd.getTime();
	}

	/**
	 * Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		if (date == null)
			return "";
		return dateFormatter.format(date).trim();

	}
	/**
	 * Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateToString1(Date date) {
		if (date == null)
			return "";
		return dateFormatter1.format(date).trim();

	}
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString2(Date date) {
		if (date == null)
			return "";
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(date).trim();

	}
	/**
	 *  Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
	 */
	public static String dateToString(Date date, String format,Locale locale) {

		SimpleDateFormat formatter = new SimpleDateFormat(format,locale);
		return formatter.format(date).trim();
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter.format(date).trim();

	}
	
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static String dateTimeToString5(Date date) {
		if (date == null)
			return "";
		return dateTimeFormatter5.format(date).trim();

	}
	
	/* Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
	 */
	public static String dateToString(Date date, String format) {

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date).trim();
	}

	/* Get the Next Date Write by Jeffy pan 2004-10-21
	 * Date Format:(YYYY-MM-DD) (YYYY:M:D HH:MM:SS)  (YYYY/M/DD hh:MM) 
	 */
	public static int getDays(String fromDate, String endDate) {

		long from = stringToDate(fromDate).getTime();
		long end = stringToDate(endDate).getTime();

		return (int) ((end - from) / (24 * 60 * 60 * 1000)) + 1;
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param fromDate
	 * @param endDate
	 * @return
	 */
	public static int getDays(Date fromDate, Date endDate) {

		long from = fromDate.getTime();
		long end = endDate.getTime();

		return (int) ((end - from) / (24 * 60 * 60 * 1000)) + 1;
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getTakeTime(Date startDate, Date endDate) {
		int minute = 0;
		try {
			minute = (int) (endDate.getTime() - startDate.getTime())
					/ (1000 * 60);
			return String.valueOf(minute);
		} catch (Exception e) {
			return "";
		}

	}

	/* 获取月份的第一天
	 * written by Sammy: 2004-10-26
	 */
	public static int getFirstDateOfMonth(int year, int month) {

		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONDAY, month - 1);
		return cl.getActualMinimum(Calendar.DAY_OF_MONTH);
	}

	/* 获取月份的最后一天
	 * written by Sammy: 2004-10-26
	 */
	public static int getLastDateOfMont(int year, int month) {
		Calendar cl = Calendar.getInstance();
		cl.set(Calendar.YEAR, year);
		cl.set(Calendar.MONDAY, month - 1);
		return cl.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static java.sql.Date convertUtilDateToSQLDate(java.util.Date date) {
		if (date == null)
			return null;
		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		java.sql.Date jd = new java.sql.Date(cl.getTimeInMillis());
		return jd;
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param dateString
	 * @return
	 */
	public static java.sql.Date convertStringToSQLDate(String dateString) {
		return (convertUtilDateToSQLDate(stringToDate(dateString)));
	}

	public static java.sql.Date convertToSQLDateWithoutTime(java.util.Date date) {
		String dateString = dateFormatter.format(date);
		return convertStringToSQLDate(dateString);
	}

	/**
	 * get offset to the previous sunday from the specific date
	 * @param from the specific start date
	 * @param from the specific end date
	 * @return offset to the previous sunday
	 * @throws ParseException
	 */
	public static List getAllSundays(Date from, Date to) {
		List sundayList = new ArrayList();
		int offset = getOffsetToNextSunday(from);
		Date firstSunday = addDate(from, offset);
		Date current = firstSunday;
		while (current.compareTo(to) <= 0) {
			sundayList.add(current);
			current = addDate(current, 7);
		}
		return sundayList;
	}

	/**
	 * get offset to the next sunday from the specific date
	 * @param date the specific date
	 * @return offset to the next sunday
	 * @throws ParseException
	 */
	public static int getOffsetToNextSunday(Date date) {
		if (getDayOfWeek2(date) == 1)
			return 0;
		return 8 - getDayOfWeek2(date);
	}

	/**
	 * get day index of a week for the specific date
	 * @param date the specific date
	 * @return day index of a week,Mon. is 1,Tues. is 2,Wed. is 3,Thurs. is 4,Fri. is 5,Sat. is 6,Sun. is 7
	 * @throws ParseException
	 */
	public static int getDayOfWeek(Date date) {
		if (date == null)
			return 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int result = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (result == 0)
			result = 7;
		return result;
	}

	/**
	 * get day index of a week for the specific date
	 * @param date the specific date
	 * @return day index of a week,Sun. is 1,Mon. is 2,Tues. is 3,Wed. is 4,Thurs. is 5,Fri. is 6,Sat. is 7
	 * @throws ParseException
	 */
	public static int getDayOfWeek2(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * get day index of a week for the specific date
	 * @param date the specific date
	 * @return day index of a week,Sun. is 1,Mon. is 2,Tues. is 3,Wed. is 4,Thurs. is 5,Fri. is 6,Sat. is 7
	 * @throws ParseException
	 */
	public static String getDayOfWeek3(Date date) {
		int day = getDayOfWeek(date);
		return days[day - 1];
	}
	/**
	 * add days to the specific date
	 * @param SourceDate the specific date
	 * @param days day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addDateMonth(Date sourceDate, int months) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.MONTH, months);
		return calendar.getTime();
	}
	/**
	 * add days to the specific date
	 * @param SourceDate the specific date
	 * @param days day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addDate(Date sourceDate, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	/**
	 * add days to the specific date
	 * @param SourceDate the specific date
	 * @param days day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addHour(Date sourceDate, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.HOUR, hours);
		return calendar.getTime();
	}
	/**
	 * add second to the specific date
	 * @param SourceDate the specific date
	 * @param seconds count to be added
	 * @return java.util.Date object after add seconds
	 * @throws ParseException
	 */
	public static Date addMinute(Date sourceDate, int Minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.MINUTE, Minute);
		return calendar.getTime();
	}
	/**
	 * add second to the specific date
	 * @param SourceDate the specific date
	 * @param seconds count to be added
	 * @return java.util.Date object after add seconds
	 * @throws ParseException
	 */
	public static Date addSecond(Date sourceDate, int second) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.SECOND, second);
		return calendar.getTime();
	}

	
	
	/**
	 * add days to the specific date
	 * @param SourceDate the specific date
	 * @param days day count to be added
	 * @return java.util.Date object after add days
	 * @throws ParseException
	 */
	public static Date addDate(String stringDate, int days) {
		Date sourceDate = stringToDate(stringDate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sourceDate);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}

	/**
	 * @param stringDate
	 * @return
	 */
	public static String addOneDay(Date sourceDate) {

		Date newDate = addDate(sourceDate, 1);
		return dateToString(newDate);

	}

	/**
	 * 
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static long subDate(Date from, Date to) throws ParseException {
		long value = Math.abs(to.getTime() - from.getTime());
		return value / MSECONDS_OF_ONE_DAY;
	}

	public static long subDateMinute(Date from, Date to) throws ParseException {
		long value = Math.abs(to.getTime() - from.getTime());
		return value / MSECONDS_OF_ONE_MINUTE;
	}
	
	/**
	 * <br>Author：zy_fu<br>
	 * <br>Date：2007-7-20<br>
	 * <br>Mender：<br>
	 * <br>Date：YYYY-MM-DD<br>
	 * <br>Version:<br>
	 * <p>计算两个时间的间隔
	 *
	 * <p>@param from
	 * <p>@param to
	 * <p>@param unitInMSecond 时间间隔的单位，1000=秒，60*1000=分钟，60*60*1000=小时...
	 * <p>@return
	 * <p>@throws ParseException
	 */
	public static long getTimespan(Date from, Date to,long unitInMSecond) throws ParseException {
		long value = Math.abs(to.getTime() - from.getTime());
		return value / unitInMSecond;
	}
	
	/**
	 * 
	 * @param from
	 * @param to
	 * @return
	 * @throws ParseException
	 */
	public static long subDate(String from, String to) throws ParseException {
		return subDate(stringToDate(from), stringToDate(to));
	}

	/**
	 * 返回时间列表 (startDate, endDate, days[])	 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param startDate
	 * @param endDate
	 * @param days
	 * @return
	 */
	public static List getStringDateList(String startDate, String endDate, int[] days) {

		List dateList = new ArrayList();

		int days2 = DateUtil.getDays(startDate, endDate)-1;
		Date fromDate2 = DateUtil.stringToDate(startDate);
		Date endDate2 = DateUtil.stringToDate(endDate);

		Calendar cal = Calendar.getInstance();

		for (int i = 0; i < days2; i++) {

			cal.setTime(fromDate2);
			cal.add(Calendar.DATE, i);

			for (int j = 0; j < days.length; j++) {
				// 星期数等于所选的
				if (days[j] == cal.get(Calendar.DAY_OF_WEEK)) {
					
					dateList.add(DateUtil.dateToString(cal.getTime()));
				}//if
			}//for

		}//for

		return dateList;
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param startDate
	 * @param endDate
	 * @param days
	 * @return
	 */
	public static List getDateList(String startDate, String endDate, int[] days) {

		List dateList = new ArrayList();

		int days2 = DateUtil.getDays(startDate, endDate);
		Date fromDate2 = DateUtil.stringToDate(startDate);
		Date endDate2 = DateUtil.stringToDate(endDate);

		Calendar cal = Calendar.getInstance();

		for (int i = 0; i < days2; i++) {

			cal.setTime(fromDate2);
			cal.add(Calendar.DATE, i);

			for (int j = 0; j < days.length; j++) {
				// 星期数等于所选的
				if (days[j] == cal.get(Calendar.DAY_OF_WEEK)) {
					dateList.add(cal.getTime());
				}//if
			}//for

		}//for

		return dateList;
	}

	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public static int compareDate(Date firstDate, Date secondDate) {
		return firstDate.compareTo(secondDate);
	}
	
	/**
	 * 
	 * Author：hxiang_xu<br>
	 * Date：2005-7-8<br>
	 * Mender：<br>
	 * Date：YYYY-MM-DD<br>
	 * Version:<br>
	 * 返回修改的时间。
	 * @param  date - 传入的时间 <code>Date</code>
	 * @param field 要修改的时间类型（Calendar.DATE，Calendar.HOUR, ...） <code>in</code>
	 * @param field 要修改的数值 <code>int</code>
	 * @return Date
	 * @exception SQLException if a database access error occurs or the SQL
	 *            statement returns a <code>ResultSet</code> object
	 *
	 * @see Connection#prepareStatement
	 * @see ResultSet
	 */
	public static Date AddDate(Date date, int field, int amount)
	{
        Calendar newCalendar = Calendar.getInstance();
        newCalendar.setTime(date);
        newCalendar.add(field, amount);
        return newCalendar.getTime();
	}
	public static int getDayDifference(Date date1, Date date2) {
		Calendar gc1 = Calendar.getInstance();
		Calendar gc2 = Calendar.getInstance();
		gc1.setTime(date1);
		gc2.setTime(date2);

		long millis = gc2.getTimeInMillis() - gc1.getTimeInMillis();
		return Math.abs((int) (millis / 1000 / 24 / 60 / 60));
	}
	
	/**
	 * Returns the difference between the two dates in days.
	 * 
	 * @param c1
	 * @param c2
	 * @return
	 */
	public static int getDayDifference(Calendar c1, Calendar c2) {
		return getDayDifference(c1.getTime(), c2.getTime());
	}
	
   /**时间差得天数
    * @param date1 <String>
    * @param date2 <String>
    * @return int
    * @throws ParseException
    */
   public int getDateSpace(String date1, String date2) {
	   int days =0;
	   try{
		   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	       Calendar calst = Calendar.getInstance();;
	       Calendar caled = Calendar.getInstance();

	       calst.setTime(sdf.parse(date1));
	       caled.setTime(sdf.parse(date2));

	        //设置时间为0时   
	        calst.set(Calendar.HOUR_OF_DAY, 0);   
	        calst.set(Calendar.MINUTE, 0);   
	        calst.set(Calendar.SECOND, 0);   
	        caled.set(Calendar.HOUR_OF_DAY, 0);   
	        caled.set(Calendar.MINUTE, 0);   
	        caled.set(Calendar.SECOND, 0);
	        //得到两个日期相差的天数   
	         days = ((int)(caled.getTime().getTime()/1000)-(int)(calst.getTime().getTime()/1000))/3600/24;   
	        
	   }catch(Exception e){
		   
	   }
      
      
       return days;   
   }
   public static  int daysBetween(Date early, Date late) { 
	     
       java.util.Calendar calst = java.util.Calendar.getInstance();   
       java.util.Calendar caled = java.util.Calendar.getInstance();   
       calst.setTime(early);   
        caled.setTime(late);   
        //设置时间为0时   
        calst.set(java.util.Calendar.HOUR_OF_DAY, 0);   
        calst.set(java.util.Calendar.MINUTE, 0);   
        calst.set(java.util.Calendar.SECOND, 0);   
        caled.set(java.util.Calendar.HOUR_OF_DAY, 0);   
        caled.set(java.util.Calendar.MINUTE, 0);   
        caled.set(java.util.Calendar.SECOND, 0);   
       //得到两个日期相差的天数   
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst   
               .getTime().getTime() / 1000)) / 3600 / 24;   
        
       return days;   
  }   
   /**
	* 获取日期年份
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getYear(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.YEAR);
	}
	
	/**
	* 获取日期月份
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getMonth(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return (calendar.get(Calendar.MONTH) + 1);
	}
	
	/**
	* 获取日期号
	* @param date
	* @return
	* @throws ParseException
	*/
	public static int getDay(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	/**
	* 获取月份起始日期
	* @param date
	* @return
	* @throws ParseException
	*/
	public static String getMinMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}
	
	/**
	* 获取月份最后日期
	* @param date
	* @return
	* @throws ParseException
	*/
	public static String getMaxMonthDate(String date) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}
	/**
	 * 区分两个日期之间指定字段的差值
	 * 
	 * @param time1
	 *            开始时间
	 * @param time2
	 *            结束时间
	 * @param field
	 *            要比较的字段(年，月，日,...)
	 * @return 如果time1>time2就反回一个正的差值,如果time1<time2则返回一个负的差值,如果相等，返回0
	 */
	public static int getFieldDifference(long time1, long time2, int field) {
		if (time1 == time2) {
			return 0;
		} else if (time1 > time2) {
			return -getFieldDifference(time2, time1, field);
		}
		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.setTimeInMillis(time1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setLenient(false);
		cal2.setTimeInMillis(time2);
		for (int x = 0; x < Calendar.FIELD_COUNT; x++) {
			if (x > field) {
				cal1.clear(x);
				cal2.clear(x);
			}
		}
		time1 = cal1.getTimeInMillis();
		time2 = cal2.getTimeInMillis();

		long ms = 0;
		int min = 0, max = 1;

		while (true) {
			cal1.setTimeInMillis(time1);
			cal1.add(field, max);
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				min = max;
				break;
			} else if (ms > time2) {
				break;
			} else {
				max <<= 1;
			}
		}

		while (max > min) {
			cal1.setTimeInMillis(time1);
			int t = (min + max) >>> 1;
			cal1.add(field, t);
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				min = t;
				break;
			} else if (ms > time2) {
				max = t;
			} else {
				min = t;
			}
		}
		return -min;
	}
	/**
	 * 
	 * 方法说明：<p></p>
	 * 创建日期：2006-4-21
	 * 创建人： Tan Nailiang
	 * @param date
	 * @return
	 */
	public static Date stringToDate1(String dateStr) {
		DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {    
	           date = format1.parse(dateStr);   
	           
	   } catch (ParseException e) {    
	           e.printStackTrace();    
	   }
	   return date;
	}
	
	public static String getTimeSoltDate(String length) {
		Date dNow = new Date();
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dNow);
		calendar.add(calendar.MONTH, -1 * Integer.valueOf(length));
		dBefore = calendar.getTime(); // 得到前3月的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String result = sdf.format(dBefore);
		return result;
	}
	
	public static String getTimeSoltDate2(String length) {
		Date dNow = new Date();
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dNow);
		calendar.add(calendar.MONTH, -1 * Integer.valueOf(length));
		dBefore = calendar.getTime(); // 得到前3月的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String result = sdf.format(dBefore);
		return result;
	}
	
	/**
	 * 获取当前日期的月份
	 * @param date
	 * @return
	 */
	public static Integer getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		return month;
	}
	
	@SuppressWarnings("finally")
	public static boolean isDateFormat(String strDate, String format){
		// 设置日期转化成功标识
		boolean dateflag = true;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try
		{
			// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			sdf.setLenient(false);
			sdf.parse(strDate);
		} catch (Exception e) {
			dateflag=false;
		} finally {
			return dateflag;
		}
	}
	
}
